package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 分院管理
 */
@RestController
@RequestMapping("/address")
@CrossOrigin
public class AddressController {


    @Reference
    private AddressService addressService;


    /**
     * 查询所有分院
     *
     * @return
     */
    @GetMapping("/findAll")
    //Result里面记得直接仿Address的数组
    public Result findAll() {
        try {
            List<Address> list = addressService.findAll();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }


    /**
     * 根据id查询分院信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findAddressById(@PathVariable Integer id) {
        try {
            Address address = addressService.findAddressById(id);
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    /**
     * 添加分院信息
     *
     * @param address
     * @return
     */
    @PostMapping("/submit")
    public Result addAddress(@RequestBody Address address) {

        try {
            addressService.addAddress(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }
    /**
     * 修改分院信息
     *
     * @param address
     * @return
     */
    @PutMapping("/update")
    public Result updateAddress(@RequestBody Address address) {
        try {
            addressService.updateAddress(address);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }
    /**
     * 删除分院信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteAddress(@PathVariable Integer id) {
        try {
            addressService.deleteAddress(id);
            return new Result(true, "删除地址成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除地址失败");
        }
    }


    /**
     * 启用禁用
     * @param id
     * @param eid
     * @return
     */
    @PutMapping("/turnByButton/{id}/{$event}")
    public Result turnByButton(@PathVariable Integer id,@PathVariable("$event") Integer eid) {
        try {
            addressService.updateAddressByeid(id,eid);

            return new Result(true, "修改分院状态成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }
}
